package com.eorchis.module.classapplydep.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;

import com.eorchis.module.classapplydep.dao.IClassApplyDepDao;
import com.eorchis.module.classapplydep.domain.ClassApplyDep;
import com.eorchis.module.classapplydep.domain.ClassApplyDepBean;
import com.eorchis.module.classapplydep.service.IClassApplyDepService;
import com.eorchis.module.classapplydep.ui.commond.ClassApplyDepQueryCommond;
import com.eorchis.module.classapplydep.ui.commond.ClassApplyDepValidCommond;
import com.eorchis.module.security.cas.domain.Department;
import com.eorchis.module.trainingclass.domain.TrainingClass;

/**
 * Title: ClassApplyDepServiceImpl<br>
 * Description: 培训班-单位关系管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author HuangTF
 * @createDate 2013-12-18
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "main.java.com.eorchis.module.classapplydep.service.impl.ClassApplyDepServiceImpl")
public class ClassApplyDepServiceImpl extends AbstractBaseService implements IClassApplyDepService {

	@Resource(name = "main.java.com.eorchis.module.classapplydep.dao.impl.ClassApplyDepDaoImpl")
	private IClassApplyDepDao classApplyDepDao;

	@Override
	public IDaoSupport getDaoSupport() 
	{
		return classApplyDepDao;
	}

	@Override
	@SuppressWarnings("unchecked")
	public ClassApplyDepValidCommond toCommond(IBaseEntity entity) 
	{
		return new ClassApplyDepValidCommond((ClassApplyDep)entity);
	}

	/**
	 * 查询未选单位列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-18
	 */
	public List<ClassApplyDepBean> findNoSelectedDepList(ClassApplyDepQueryCommond queryCommond)
			throws Exception
	{
		return classApplyDepDao.findNoSelectedDepList(queryCommond);
	}
	/**
	 * 添加单位
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-19
	 */
	@Override
	public String saveApplyDep(ClassApplyDepQueryCommond queryCommond) throws Exception 
	{
		String[] depIds=queryCommond.getDepIds();
		for(int i=0;i<depIds.length;i++)
		{
			ClassApplyDep classApplyDep=new ClassApplyDep();
			
			TrainingClass trainingClass=new TrainingClass();
			trainingClass.setClassId(queryCommond.getClassId());
			
			Department department=new Department();
			department.setDeptID(depIds[i]);
			
			classApplyDep.setTrainingClass(trainingClass);
			classApplyDep.setDepartment(department);
			classApplyDep.setRestrictPersonNum(0l);
			
			classApplyDepDao.save(classApplyDep);
		}
		return "添加成功";
	}
	/**
	 * 更新已选单位限制人数
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-19
	 */
	public String updatePersonNum(ClassApplyDepQueryCommond queryCommond)
			throws Exception
	{
		String mess="";
		ClassApplyDepValidCommond c=this.find(queryCommond.getSearchClassApplydepId());
		
		Long personNum=classApplyDepDao.findPersonNum(queryCommond);//全部已选单位可报人数
		Long restrictPersonNum=queryCommond.getSearchRestrictPersonNum();//需要修改的可报名人数
		int classUserNum=c.getTrainingClass().getClassUserNum();//班级最大人数
		if(personNum-c.getRestrictPersonNum()+restrictPersonNum<=classUserNum)
		{
			classApplyDepDao.updatePersonNum(queryCommond);
			mess="更改成功";
		}
		else
		{
			mess="超过班级人数，当前班级剩余名额："+(classUserNum-personNum);
		}
		return mess;
	}
}
